package leetcode.editor.cn;
 
/**
 * 长度最小的子数组
 * @author qulq
 * @date 2024-07-06 11:16:41
 */
public class T209_MinimumSizeSubarraySum{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T209_MinimumSizeSubarraySum().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int minSubArrayLen(int target, int[] nums) {

		int ans = Integer.MAX_VALUE, sum = 0;
		for (int left = 0, right = 0; right < nums.length; right++) {
			sum += nums[right];
			while (sum >= target) {
				// 注意：！！！！
				// 求最小值的滑动窗口和最大值的区别就是统计答案的位置不同
				ans = Math.min(ans, right - left + 1);
				sum -= nums[left++];
			}
		}
		return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}